package com.example.demo.service.impl;

import com.alibaba.fastjson2.JSON;
import com.example.demo.mapper.InvoiceMapper;
import com.example.demo.pojo.Invoice;
import com.example.demo.pojo.InvoiceNumber;
import com.example.demo.service.InvoiceService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.ibatis.session.SqlSession;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.sql.PreparedStatement;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class InvoiceSeriveImpl implements InvoiceService {

    @Autowired
    private InvoiceMapper invoiceMapper;


    @Override
    public InvoiceNumber selectOnly(String fphm,String fpbm) {
        String ps = fphm+fpbm;
        return invoiceMapper.selectOne(ps);
    }



    @Override
    @Scheduled(cron = "0 0 12 * * ?")
    public void test() {
        List<InvoiceNumber> invoiceNumberList = new ArrayList<>();
        // 创建RestTemplate实例
        RestTemplate restTemplate = new RestTemplate();
        String formattedDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        // 设置请求参数
        MultiValueMap<String,Object> map = new LinkedMultiValueMap<>();
        map.add("token", "aeaf98bfc114474383315c61eec155b9uuph2pxwzl");
        map.add("userid", "c4593f24e3d846de99e5ea7ebad7fdb3");
        map.add("spid","ee79fbeca7f9f68ae5996c3ae5b016bd");
        map.add("begintime","2023-10-01");
        map.add("endtime",formattedDate);
        map.add("pageindex",1);
        map.add("pagesize",300);

        // 发送POST请求并获取响应结果
        String url = "https://fp.xinpukeji.cn/wechatapi/NEWKP/JXFPAPI/JXMX";
        String s = restTemplate.postForObject(url, map, String.class);
        JSONObject jsonObject = new JSONObject(s);
        Gson gson = new Gson();
        JSONArray rows = jsonObject.getJSONArray("rows");
        List<Invoice> invoiceList = gson.fromJson(String.valueOf(rows), new TypeToken<List<Invoice>>(){}.getType());
        invoiceList.forEach(i->{
            InvoiceNumber invoiceNumber = new InvoiceNumber();
            invoiceNumber.setFPHM(i.getFPHM());
            invoiceNumberList.add(invoiceNumber);
        });

        System.out.println("执行成功！！！");
        invoiceNumberList.forEach(i->{
            invoiceMapper.InsertIntoNumber(i);
        });

    }


}
